package com.example.tianling.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.annotation.LoginUser;
import com.example.tianling.common.utils.R;
import com.example.tianling.common.vo.bookListVo;
import com.example.tianling.common.vo.materialCollectionListVo;
import com.example.tianling.entity.MaterialCollectionsEntity;
import com.example.tianling.entity.UserBookMarksEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.BookService;
import com.example.tianling.service.MaterialCollectionsService;
import com.example.tianling.service.UserBookMarksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

@Controller
public class BookAndMaterialCollectionController {
    @Autowired
    private UserBookMarksService userBookMarksService;
    @Autowired
    private MaterialCollectionsService materialCollectionsService;
    @Login
    @PostMapping(value="/Collection/bookColection.do")
    @ResponseBody
    public R bookColection(@RequestBody UserBookMarksEntity userBookMarksEntity, @LoginUser UserEntity user) throws ParseException {
        Integer resule = userBookMarksService.setBookMark(userBookMarksEntity,user.getUserId());
        if(resule==1){
            return R.success("收藏成功");
        }
        else if(resule==-2) {
            return R.error(404,"您已收藏");
        }
        else {
            return R.error(404,"收藏失败");
        }
    }

    @Login
    @PostMapping(value="/Collection/getMyBookCollection.do")
    @ResponseBody
    public R getMyBookCollection(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String pageNum = paramsJSONObject.getString("pageNum");
        String type = paramsJSONObject.getString("type");
        List<bookListVo> userBookMarksEntities = userBookMarksService.getMyBookMark(user.getUserId(), Integer.valueOf(pageNum),type);
        return R.success(userBookMarksEntities);
    }

    @Login
    @PostMapping(value="/Collection/cancelMyBookCollection.do")
    @ResponseBody
    public R cancelMyBookCollection(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String bookId = paramsJSONObject.getString("bookId");
        Integer result = userBookMarksService.cancelMyBookCollection(user.getUserId(), Integer.valueOf(bookId));
        if(result==1){
            return R.success("取消收藏成功");
        }
        else {
            return R.error(404,"取消收藏失败");
        }
    }

    @Login
    @PostMapping(value="/Collection/materialColection.do")
    @ResponseBody
    public R materialColection(@RequestBody MaterialCollectionsEntity materialCollectionsEntity, @LoginUser UserEntity user) throws ParseException {
        Integer resule = materialCollectionsService.setMaterialCollection(materialCollectionsEntity,user.getUserId());
        if(resule==1){
            return R.success("收藏成功");
        }
        else if(resule==-2) {
            return R.error(404,"您已收藏");
        }
        else {
            return R.error(404,"收藏失败");
        }
    }

    @Login
    @PostMapping(value="/Collection/getMyMaterialCollection.do")
    @ResponseBody
    public R getMyMaterialCollection(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String pageNum = paramsJSONObject.getString("pageNum");

        List<materialCollectionListVo> materialCollections = materialCollectionsService.getMyMaterialCollections(user.getUserId(), Integer.valueOf(pageNum));
        return R.success(materialCollections);
    }

    @Login
    @PostMapping(value="/Collection/cancelMyMAterialCollection.do")
    @ResponseBody
    public R cancelMyMAterialCollection(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String materialId = paramsJSONObject.getString("materialId");
        Integer result = materialCollectionsService.cancelMyMaterialCollection(user.getUserId(), Integer.valueOf(materialId));
        if(result==1){
            return R.success("取消收藏成功");
        }
        else {
            return R.error(404,"取消收藏失败");
        }
    }
    @Login
    @PostMapping(value="/Collection/isCollectBook.do")
    @ResponseBody
    public R isCollectBook(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String bookId = paramsJSONObject.getString("bookId");
        Integer result = userBookMarksService.iscollectBook(user.getUserId(), Integer.valueOf(bookId));
        return R.success(result);
    }

    @Login
    @PostMapping(value="/Collection/isCollectMaterial.do")
    @ResponseBody
    public R isCollectMaterial(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String materialId = paramsJSONObject.getString("materialId");
        Integer result = materialCollectionsService.isCollectMaterial(user.getUserId(), Integer.valueOf(materialId));
        return R.success(result);
    }
}
